home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: Allocating memory in windows programs
- Date: 21 Mar 1996 11:49:59 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4irfp7$g0u@news1.h1.usa.pipeline.com>
- References: <4ir0j0$2ge@dsk92.itg.ti.com>
- NNTP-Posting-Host: 38.8.120.9
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 21, 1996 07:30:40 in article <Allocating memory in windows
- programs>, 'dwnw%mimi@magic.itg.ti.com (Doug Nintzel)' wrote:
-
-
- >Hello, I am new at windows programming. I am using MS-SDK/c++. I am using
- the
- >below code to allocate memory....
- >/****************************************************************************/
- >memtst()
- >{
- >char FAR*str;
- >i=0;
- >
- >/*
- >if((str=(char*)malloc(sizev))==NULL){MessageBox(hWnd,"GlobLock()failed","globl
- >ock",MB_OK);return(0);}
- >for(i=0;i<555;++i)str[i]='x';str[25]='\0';
- >*/
- >if((hmem=GlobalAlloc(GHND,100))==NULL){MessageBox(hWnd,"GlobAlloc(),
- > failed",MB_OK);return(0);}
- >str=(char FAR*)GlobalLock(hmem);
- >if(str==NULL){MessageBox(hWnd,"GlobLock()failed","globlock",MB_OK);return(0);}
- >MessageBox(hWnd,str,"ALLOC/LOCK OK",MB_OK);
- >strcpy(str,"mem test ok");
- >MessageBox(hWnd,str,"str content",MB_OK);
- >}
- >/****************************************************************************/
- >...it crashes at strcpy(str,"mem test ok");...
- >
- >I have also tried to used the commented out malloc() test instead of
- >GlobalAlloc().
- >I have tried several other changes/combinations w/no luck.
- >I have also checked c++ FAQ's and checked several book stores.
- >I am running out of ideas!!!...Please help.....
- >
- Most likely cause: You're building with a small or equivalent
- memory model. Standard solution is to switch to a large model,
- however, some compilers have an alternate form of str.. functions
- of the form fstr.. that accept far pointers. IMHO you should
- use the large memory model to avoid problems elsewhere.
-
- FYI: The small MM library calls accept only near pointers.
- GlobalLock returns a far pointer. When you pass a far pointer
- (doesn't you compiler warn about this??) where a near one is
- expected, you end up with an address that is probably invalid --
- hence the crash.
-
- I see nothing else wrong in your sample code so it sure looks
- like a memory model mismatch problem.
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-